<template>
	<view>
		<u-navbar title="通知" :background="navBg" :borderBottom="false" titleColor="#fff" backIconColor="#fff"></u-navbar>
		<view class="uni-flex uni-column align-center t-mg-2">
			<text>关注公众号，以免错过重要通知</text>
			<image class="img-qrcode t-mg b-mg" src="static/img/pub_qrcode.jpg" mode="widthFix"></image>
			<!-- #ifdef MP-WEIXIN -->
			<u-button shape="circle" type="primary" @click="save">保存到相册</u-button>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				navBg: {
					'background-image': 'linear-gradient(to right, #434AD8, #8E77F5)'
				},
			}
		},
		methods: {
			// #ifdef MP-WEIXIN
			async save() {
				uni.showLoading({
					mask: true
				})
				try {
					await new Promise((resolve, reject) => {
						uni.getSetting({
							success: (res) => {
								let t = res.authSetting["scope.writePhotosAlbum"];
								if (t !== undefined && !t) {
									// 请求过权限，但是用户关闭了
									uni.showModal({
										title: "请先打开相册开关",
										cancelText: "取消",
										confirmText: "去打开",
										success: (res) => {
											if (res.confirm) {
												uni.openSetting();
											}
										}
									})
									rejcet();
								} else {
									resolve();
								}
							},
							fail: (err) => {
								resolve();
							}
						})
					})
				} catch (e) {
					uni.hideLoading();
					return;
				}

				let imagePath;
				try {
					imagePath = await new Promise((resolve, reject) => {
						uni.getImageInfo({
							src: "./static/img/pub_qrcode.jpg",
							success: (image) => {
								resolve(image.path);
								console.log('获取图片成功', image)
							},
							fail: (err) => {
								reject(err);
								console.log('获取图片失败', err)
							}
						});
					});
				} catch (e) {
					this.$log("获取二维码路径遇到错误", e);
					uni.hideLoading();
					return;
				}

				uni.saveImageToPhotosAlbum({
					filePath: imagePath,
					success: (res) => {
						uni.hideLoading();
						uni.showToast({
							title: '已保存到相册',
							icon: 'success',
							duration: 2000
						})
					},
					fail: (e) => {
						uni.showToast({
							title: '保存失败',
							icon: 'none',
							duration: 2000
						})
						uni.hideLoading();
					}
				})
			},
			// #endif
		}
	}
</script>

<style lang="scss">
	.img-qrcode {
		width: 50%;
	}
</style>
